package cn.wolfcode.controller;

import cn.wolfcode.Service.IProductService;
import cn.wolfcode.domain.Product;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController {
    private final IProductService productService;

    @Value("${server.port}")
    private String port;

    @RequestMapping("{pid}")
    public Product findByPid(@PathVariable("pid") Long pid){
        log.info("接下来要进行{}号商品信息的查询", pid);
        Product product = productService.getById(pid);
        // 为了更直观看到请求是进行负载均衡了,这里的商品名称加上端口号
        product.setName(product.getName()+"-"+port);
        log.info("商品信息查询成功,内容为{}", JSON.toJSONString(product));
        return product;
    }
}
